package com.wangshb.upms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangshb.upms.core.common.BeanMapper;
import com.wangshb.upms.core.common.PageRequest;
import com.wangshb.upms.dto.PageResponseDto;
import com.wangshb.upms.dto.upms.user.*;
import com.wangshb.upms.entity.UpmsUser;
import com.wangshb.upms.service.UpmsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


/**
 * @author auto code
 */
@RestController
public class UpmsUserController {

    @Autowired
    private UpmsUserService upmsUserService;


    @PostMapping("/upms/user/listPage")
    public PageResponseDto listPage(@RequestBody PageRequest<UpmsUserQueryRequestDto> page) {
        Page<UpmsUser> resultPage = this.upmsUserService.findPage(page.getPage(), page.getLimit(), BeanMapper.map(page.getCondition(), UpmsUser.class));
        return PageResponseDto.builder().total(resultPage.getTotal()).data(BeanMapper.mapAsList(resultPage.getRecords(), UpmsUserQueryResponseDto.class)).build();
    }


    @PostMapping("/upms/user/add")
    public Long addUpmsUser(@RequestBody UpmsUserAddRequestDto addDto) {
        return this.upmsUserService.addUpmsUser(BeanMapper.map(addDto, UpmsUser.class));
    }


    @PostMapping("/upms/user/edit")
    public void editUpmsUser(@RequestBody UpmsUserEditRequestDto editDto) {
        this.upmsUserService.updateUpmsUserWithLock(BeanMapper.map(editDto, UpmsUser.class));
    }


    @PostMapping("/upms/user/del")
    public void delUpmsUser(@RequestBody UpmsUserDelRequestDto delDto) {
        this.upmsUserService.delUpmsUser(BeanMapper.map(delDto, UpmsUser.class));
    }

    @PostMapping("/upms/user/batch/del")
    public void batchDelUpmsUser(@Valid @RequestBody UpmsUserBatchDelRequestDto batchDelRequestDto) {
        this.upmsUserService.batchDelUpmsUser(batchDelRequestDto.getIdList());
    }

    @PostMapping("/upms/user/get")
    public UpmsUserGetResponseDto findById(@RequestBody UpmsUserGetRequestDto getDto) {
        QueryWrapper<UpmsUser> upmsUserQueryWrapper = new QueryWrapper<>();
        upmsUserQueryWrapper.setEntity(BeanMapper.map(getDto, UpmsUser.class));
        UpmsUser upmsUser = this.upmsUserService.getOne(upmsUserQueryWrapper);
        return null == upmsUser ? null : BeanMapper.map(upmsUser, UpmsUserGetResponseDto.class);
    }

    /**
     * 重置密码
     * @param resetPasswordRequestDto
     * @return
     */
    @PostMapping("/upms/user/reset/auth")
    @Validated
    public void resetPassword(@Valid @RequestBody UpmsUserResetPasswordRequestDto resetPasswordRequestDto) {
        this.upmsUserService.resetPassword(BeanMapper.map(resetPasswordRequestDto, UpmsUser.class));
    }

}
